﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.View.congno
{
    public partial class dscongno : UserControl
    {
        public static string thangnamload = "";

        DataView view = new DataView();
        public dscongno()
        {
            InitializeComponent();
        }

        int currentPageIndex = 1;
        int pageSize = 22;
        int pageNumber = 1;
        int fistRow, lastRow;
        int rows;

        private void binCombo(ComboBox cb, DataTable dt, string name, string value, string option)
        {
            List<trangthai> list = new List<trangthai>();
            trangthai ls = null;
            ls = new trangthai();
            ls.name = option;
            ls.value = "0";
            list.Add(ls);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ls = new trangthai();
                    ls.name = dt.Rows[i][name].ToString();
                    ls.value = dt.Rows[i][value].ToString();
                    list.Add(ls);
                }
            }
            cb.DataSource = list;
            cb.DisplayMember = "name";
            cb.ValueMember = "value";
        }

        private void dscongno_Load(object sender, EventArgs e)
        {
            clDHTong cldht = new clDHTong();
            AutoCompleteStringCollection dsdht = new AutoCompleteStringCollection();

            DataTable dtds = cldht.getdata(1);
            if (dtds != null && dtds.Rows.Count > 0)
            {
                foreach (DataRow row in dtds.Rows)
                {
                    dsdht.Add(row["tenDHTong"].ToString());
                }
            }

            cbbDHT.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cbbDHT.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbbDHT.AutoCompleteCustomSource = dsdht;
            //end gợi ý DHT

            //load gợi ý phường xã
            clPX clpx = new clPX();
            AutoCompleteStringCollection dspx = new AutoCompleteStringCollection();

            DataTable dtpxl = clpx.getdata(1, 0);
            if (dtpxl != null && dtpxl.Rows.Count > 0)
            {
                foreach (DataRow row in dtpxl.Rows)
                {
                    dspx.Add(row["ten"].ToString());
                }
            }

            cbbPhuong.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cbbPhuong.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cbbPhuong.AutoCompleteCustomSource = dspx;
            //end gợi ý phường xã

            clDHTong dht = new clDHTong();
            DataTable dtdht = dht.getdata(0);
            binCombo(cbbDHT, dtdht, "tenDHTong", "id", "<Chọn ĐHT>");
            clPX px = new clPX();
            DataTable dtpx = px.getdata(0, 0);
            binCombo(cbbPhuong, dtpx, "ten", "id", "<Chọn Phường>");
            DataTable dt = new DataTable();
            binCombo(cbbPho, dt, "ten", "id", "<Chọn Phố>");
        }

        void pagetotal(string thangnam, Int16 idDHT, Int16 idPh, Int16 idPho)
        {
            clTaoTbSuDung clsd = new clTaoTbSuDung();
            rows = clsd.countrow(thangnam, idDHT, idPh, idPho);

            pageNumber = rows % pageSize != 0 ? rows / pageSize + 1 : rows / pageSize;
            lbtotalpage.Text = "/" + pageNumber.ToString();
            cbbpage.Items.Clear();
            for (int i = 1; i <= pageNumber; i++)
            {
                cbbpage.Items.Add(i + "");
            }
        }

        private void nuberricPagesize_ValueChanged(object sender, EventArgs e)
        {
            //them ngày 14/2
            int thang = int.Parse(nbrThang.Value.ToString());
            int nam = int.Parse(nbrnam.Value.ToString());
            //string thangnamload = "";
            if (thang < 10)
            {
                thangnamload = "tbSudung" + nam + "0" + thang;
            }
            else
            {
                thangnamload = "tbSudung" + nam + "" + thang;
            }
            //end
            //string thangnamload = tbSudungnew;
            Int16 idDHtong = Int16.Parse(cbbDHT.SelectedValue.ToString());
            Int16 idPhuongx = Int16.Parse(cbbPhuong.SelectedValue.ToString());
            Int16 idP = Int16.Parse(cbbPho.SelectedValue.ToString());

            pageSize = Convert.ToInt32(nuberricPagesize.Value);
            pagetotal(thangnamload, idDHtong, idPhuongx, idP);
            pageNumber = int.Parse(cbbpage.Text);

            clTaoTbSuDung clsd = new clTaoTbSuDung();
            DataTable dtsd = clsd.loadDataSuDung(thangnamload, pageNumber, pageSize, idDHtong, idPhuongx, idP);
            dataGridViewCongNo.AutoGenerateColumns = false;
            dataGridViewCongNo.DataSource = view = dtsd.DefaultView;

            foreach (DataGridViewRow row in dataGridViewCongNo.Rows)
            {
                if (Int16.Parse(row.Cells["trangThaiNo"].Value.ToString()) == 1)
                {
                    row.DefaultCellStyle.BackColor = Color.SkyBlue;
                }
            }

            //disable cell
            foreach (DataGridViewRow row1 in dataGridViewCongNo.Rows)
            {
                if (Int16.Parse(row1.Cells["trangThaiNo"].Value.ToString()) == 1)
                {
                    row1.Cells["maCu"].ReadOnly = true;
                    row1.Cells["tenKH"].ReadOnly = true;
                    row1.Cells["socu2"].ReadOnly = true;
                    row1.Cells["somoi2"].ReadOnly = true;
                    row1.Cells["saukm"].ReadOnly = true;
                    row1.Cells["thue"].ReadOnly = true;
                    row1.Cells["phint"].ReadOnly = true;
                    row1.Cells["tongtien"].ReadOnly = true;
                    row1.Cells["trangThaiNo"].ReadOnly = true;
                }
                else
                {
                    row1.Cells["maCu"].ReadOnly = true;
                    row1.Cells["tenKH"].ReadOnly = true;
                    row1.Cells["socu2"].ReadOnly = true;
                    row1.Cells["somoi2"].ReadOnly = true;
                    row1.Cells["saukm"].ReadOnly = true;
                    row1.Cells["thue"].ReadOnly = true;
                    row1.Cells["phint"].ReadOnly = true;
                    row1.Cells["tongtien"].ReadOnly = true;
                }
            }
        }

        private void btnhienthi_Click(object sender, EventArgs e)
        {
            //them ngày 14/2
            int thangload = int.Parse(nbrThang.Value.ToString());
            int namload = int.Parse(nbrnam.Value.ToString());
            //string thangnamload = "";
            if (thangload < 10)
            {
                thangnamload = "tbSudung" + namload + "0" + thangload;
            }
            else
            {
                thangnamload = "tbSudung" + namload + "" + thangload;
            }
            //end
            //string thangnamload = tbSudungnew;
            cbbpage.Text = "1";
            //string thangnamload = dateTimeThangnam.Value.ToString("MM/dd/yyyy");
            int nam = int.Parse(thangnamload.Substring(8, 4));
            int thang = int.Parse(thangnamload.Substring(12, thangnamload.Length - 12));
            lb_thangsudung.Text = "Đang quản lý công nợ tháng " + thang + " năm " + nam + "";

            Int16 idDHtong = Int16.Parse(cbbDHT.SelectedValue.ToString());
            Int16 idPhuongx = Int16.Parse(cbbPhuong.SelectedValue.ToString());
            Int16 idP = Int16.Parse(cbbPho.SelectedValue.ToString());

            clTaoTbSuDung clsd = new clTaoTbSuDung();
            //rows = clsd.countrow(thangnamload,idDHtong,idPhuongx,idP);

            pagetotal(thangnamload, idDHtong, idPhuongx, idP);

            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            DataTable dtsd = clsd.loadDataSuDung(thangnamload, pageNumber, pageSize, idDHtong, idPhuongx, idP);
            dataGridViewCongNo.AutoGenerateColumns = false;
            dataGridViewCongNo.DataSource = view = dtsd.DefaultView;
            dataGridViewCongNo.ReadOnly = false;

            //set color cho rows là khách hàng nợ
            foreach (DataGridViewRow row in dataGridViewCongNo.Rows)
            {
                if (Int16.Parse(row.Cells["trangThaiNo"].Value.ToString()) == 1)
                {
                    row.DefaultCellStyle.BackColor = Color.SkyBlue;
                }
            }
            //disable cell
            foreach (DataGridViewRow row1 in dataGridViewCongNo.Rows)
            {
                if (Int16.Parse(row1.Cells["trangThaiNo"].Value.ToString()) == 1)
                {
                    row1.Cells["maCu"].ReadOnly = true;
                    row1.Cells["tenKH"].ReadOnly = true;
                    row1.Cells["socu2"].ReadOnly = true;
                    row1.Cells["somoi2"].ReadOnly = true;
                    row1.Cells["saukm"].ReadOnly = true;
                    row1.Cells["thue"].ReadOnly = true;
                    row1.Cells["phint"].ReadOnly = true;
                    row1.Cells["tongtien"].ReadOnly = true;
                    row1.Cells["trangThaiNo"].ReadOnly = true;
                }
                else
                {
                    row1.Cells["maCu"].ReadOnly = true;
                    row1.Cells["tenKH"].ReadOnly = true;
                    row1.Cells["socu2"].ReadOnly = true;
                    row1.Cells["somoi2"].ReadOnly = true;
                    row1.Cells["saukm"].ReadOnly = true;
                    row1.Cells["thue"].ReadOnly = true;
                    row1.Cells["phint"].ReadOnly = true;
                    row1.Cells["tongtien"].ReadOnly = true;
                }
            }
        }

        private void cbbpage_SelectedIndexChanged(object sender, EventArgs e)
        {
            //them ngày 14/2
            int thangload = int.Parse(nbrThang.Value.ToString());
            int namload = int.Parse(nbrnam.Value.ToString());
            //string thangnamload = "";
            if (thangload < 10)
            {
                thangnamload = "tbSudung" + namload + "0" + thangload;
            }
            else
            {
                thangnamload = "tbSudung" + namload + "" + thangload;
            }
            //end
            //string thangload = dateTimeThangnam.Value.ToString("MM/dd/yyyy");
            //string thangnamload = tbSudungnew;
            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            Int16 idDHtong = Int16.Parse(cbbDHT.SelectedValue.ToString());
            Int16 idPhuongx = Int16.Parse(cbbPhuong.SelectedValue.ToString());
            Int16 idP = Int16.Parse(cbbPho.SelectedValue.ToString());

            //currentPageIndex = Convert.ToInt32(cbbpage.Text);
            //fistRow = pageSize * (currentPageIndex - 1); //Dong dau
            //lastRow = pageSize * (currentPageIndex);//Dong cuoi cua 1 trang duoc chon.


            clTaoTbSuDung clsd = new clTaoTbSuDung();
            DataTable dtsd = clsd.loadDataSuDung(thangnamload, pageNumber, pageSize, idDHtong, idPhuongx, idP);
            dataGridViewCongNo.AutoGenerateColumns = false;
            dataGridViewCongNo.DataSource = view = dtsd.DefaultView;

            foreach (DataGridViewRow row in dataGridViewCongNo.Rows)
            {
                if (Int16.Parse(row.Cells["trangThaiNo"].Value.ToString()) == 1)
                {
                    row.DefaultCellStyle.BackColor = Color.SkyBlue;
                }
            }

            //disable cell
            foreach (DataGridViewRow row1 in dataGridViewCongNo.Rows)
            {
                if (Int16.Parse(row1.Cells["trangThaiNo"].Value.ToString()) == 1)
                {
                    row1.Cells["maCu"].ReadOnly = true;
                    row1.Cells["tenKH"].ReadOnly = true;
                    row1.Cells["socu2"].ReadOnly = true;
                    row1.Cells["somoi2"].ReadOnly = true;
                    row1.Cells["saukm"].ReadOnly = true;
                    row1.Cells["thue"].ReadOnly = true;
                    row1.Cells["phint"].ReadOnly = true;
                    row1.Cells["tongtien"].ReadOnly = true;
                    row1.Cells["trangThaiNo"].ReadOnly = true;
                }
                else
                {
                    row1.Cells["maCu"].ReadOnly = true;
                    row1.Cells["tenKH"].ReadOnly = true;
                    row1.Cells["socu2"].ReadOnly = true;
                    row1.Cells["somoi2"].ReadOnly = true;
                    row1.Cells["saukm"].ReadOnly = true;
                    row1.Cells["thue"].ReadOnly = true;
                    row1.Cells["phint"].ReadOnly = true;
                    row1.Cells["tongtien"].ReadOnly = true;
                }
            }
        }

        private void cbbPhuong_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Int16 pxid = Int16.Parse((cbbPhuong.SelectedValue == null) ? "0" : cbbPhuong.SelectedValue.ToString().Trim());
                if (pxid > 0)
                {
                    clPho p = new clPho();
                    AutoCompleteStringCollection dsp = new AutoCompleteStringCollection();
                    DataTable dtp = p.getdata(1, pxid);
                    binCombo(cbbPho, dtp, "ten", "id", "<Chọn Phố>");

                    if (dtp != null && dtp.Rows.Count > 0)
                    {
                        foreach (DataRow row in dtp.Rows)
                        {
                            dsp.Add(row["ten"].ToString());
                        }
                    }

                    cbbPho.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    cbbPho.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    cbbPho.AutoCompleteCustomSource = dsp;
                }
            }
            catch { }
        }

        private void dataGridViewCongNo_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //them ngày 14/2
            int thangload = int.Parse(nbrThang.Value.ToString());
            int namload = int.Parse(nbrnam.Value.ToString());
            //string thangnamload = "";
            if (thangload < 10)
            {
                thangnamload = "tbSudung" + namload + "0" + thangload;
            }
            else
            {
                thangnamload = "tbSudung" + namload + "" + thangload;
            }

            Int64 idkh = Int64.Parse(dataGridViewCongNo.Rows[int.Parse(dataGridViewCongNo.CurrentRow.Index.ToString())].Cells["idkh"].Value.ToString());
            //Int16 trangthaiNo = Int16.Parse((Convert.ToBoolean(Convert.ToInt16(dataGridViewCongNo.Rows[int.Parse(dataGridViewCongNo.CurrentRow.Index.ToString())].Cells["trangThaiNo"].Value.ToString())) == true) ? "1" : "0");
            Int16 trangthaiNo = 0;
            if (dataGridViewCongNo.Rows[int.Parse(dataGridViewCongNo.CurrentRow.Index.ToString())].Cells["trangThaiNo"].Value != DBNull.Value && Convert.ToBoolean(dataGridViewCongNo.Rows[int.Parse(dataGridViewCongNo.CurrentRow.Index.ToString())].Cells["trangThaiNo"].Value) == true)
            {
                trangthaiNo = 1;
                dataGridViewCongNo.Rows[int.Parse(dataGridViewCongNo.CurrentRow.Index.ToString())].DefaultCellStyle.BackColor = Color.SkyBlue;
            }
            else
            {
                trangthaiNo = 0;
                dataGridViewCongNo.Rows[int.Parse(dataGridViewCongNo.CurrentRow.Index.ToString())].DefaultCellStyle.BackColor = Color.White;
            }

            clCongNo clcongno = new clCongNo();
            clcongno.themKhachHangNo(thangnamload, idkh, trangthaiNo);

            foreach (DataGridViewRow row1 in dataGridViewCongNo.Rows)
            {
                    row1.Cells["maCu"].ReadOnly = true;
                    row1.Cells["tenKH"].ReadOnly = true;
                    row1.Cells["socu2"].ReadOnly = true;
                    row1.Cells["somoi2"].ReadOnly = true;
                    row1.Cells["saukm"].ReadOnly = true;
                    row1.Cells["thue"].ReadOnly = true;
                    row1.Cells["phint"].ReadOnly = true;
                    row1.Cells["tongtien"].ReadOnly = true;
            }
        }
    }
}
